//
//  ANRegisterController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANRegisterController.h"
#import "inputCell.h"
#import "RegisterFooterView.h"
#import "ANVIPRegisterController.h"

@interface ANRegisterController ()<InputCellTableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) RegisterFooterView *footerView;

@property (nonatomic, copy) NSString *tel;
@property (nonatomic, copy) NSString *psd;
@property (nonatomic, copy) NSString *gender;
@property (nonatomic, copy) NSString *orgName;

@end

@implementation ANRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册";
}

- (void)requestData
{
    if (!self.tel || self.tel.length < 1) {
        [MBProgressHUD showWarning:@"请输入手机号"];
        return;
    }
    if (!self.psd || self.psd.length < 1) {
        [MBProgressHUD showWarning:@"请输入密码"];
        return;
    }
    if (!self.orgName || self.orgName.length < 1) {
        [MBProgressHUD showWarning:@"请输入姓名"];
        return;
    }
    [MBProgressHUD showActivityMessage:@"正在注册中..."];
    NSDictionary *para = @{
                           @"telephone":self.tel,
                           @"password":self.psd,
                           @"gender":StringNone(self.gender),
                           @"orgName":self.orgName
                           };
    [URLManager.shareManager postUrlStr:Api_User_Register parameters:para success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                [MBProgressHUD showSuccess:@"注册成功"];
                // 保存账号
                [UserUtils.shared saveTelString:self.tel];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }

    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

- (void)setupSubViews
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.footerView = [[RegisterFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
    self.tableView.tableFooterView = self.footerView;
    WeakSelf
    self.footerView.weiVipHandler = ^{
        [weakSelf requestData];
    };
//    self.footerView.VipHandler = ^{
//        ANVIPRegisterController *vc = [[ANVIPRegisterController alloc] init];
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//    };
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    inputCell *cell = [tableView dequeueReusableInputCellWithIdentifier:@"inputCell"];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"手机号码";
            cell.placeHolderString = @"请输入手机号码";
            cell.keyboardType = UIKeyboardTypeNumberPad;
            cell.text = self.tel;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"密码";
            cell.placeHolderString = @"请输入密码";
            cell.text = self.psd;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"姓名";
            cell.placeHolderString = @"请输入姓名";
            cell.text = self.orgName;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"性别";
            cell.editable = NO;
            cell.text = @"请选择性别(可选)";
            if (self.gender) {
                if ([self.gender isEqualToString:@"1"]) {
                    cell.text = @"男";
                }
                else {
                    cell.text = @"女";
                }
            }
            
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectTextViewAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 1) {
        LLAlertViewController *vc = [[LLAlertViewController alloc] init];
        vc.actions = @[@"男",@"女"];
        vc.actionBlock = ^(NSInteger index, NSString *actionTitle) {
            if (index == 0) {
                self.gender = @"1";
            }
            else {
                self.gender = @"2";
            }
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
        };
        [vc presentBottomViewControllerFromController:self];
    }
}

- (void)tableView:(UITableView *)tableView updataText:(NSString *)text indexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            self.tel = text;
        }
        if (indexPath.row == 1) {
            self.psd = text;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            self.orgName = text;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 50;
        _tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
